<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: create_pdf.php 224 2013-09-22 10:20:51Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 224 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-22 10:20:51 +0000 (Sun, 22 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

require_once('includes/application_top.php');

require_once(DIR_FS_INC.'inc.get_order_data.php');
require_once(DIR_FS_INC.'inc.get_attributes_model.php');

require_once(DIR_FS_INC.'inc.format_price_order.php');
require_once(DIR_FS_INC.'inc.build_order_id.php');

$order = new order($_GET['oID']);

require(DIR_FS_CATALOG.DIR_WS_CLASSES.'class.price.php');
$pdf_price = new price($order->info['currency'], $_SESSION['customers_status']['customers_status_id'], $db);

$type = $_POST['type'];
$type = $_GET['type'];

$sprach_id = $_POST['pdf_language_id'];
$sprache = $order->info['language'];

require(DIR_WS_FUNCTIONS.'pdf_bill.php');
$pdf = new PDF_Bill();

$pdf->Init(($type=='rechnung'?FILENAME_BILL:FILENAME_PACKINSLIP));

// Kunden ID abfragen
$order_check = $db->db_query("SELECT customers_id FROM ".TABLE_ORDERS." WHERE orders_id='".(int)$_GET['oID']."'");
$customers_id = $order_check->fields['customers_id'];

$customer_gender = $db->db_query("SELECT customers_gender FROM ".TABLE_CUSTOMERS." WHERE customers_id='".(int)$customers_id."'");

if(PDF_LIEFERSCHEIN)
	$kundenadresse = address_format($order->customer['format_id'], $order->delivery, 1, '', '<br>');
else
	$kundenadresse = address_format($order->customer['format_id'], $order->billing, 1, '', '<br>');

$pdf->Adresse(utf8_decode(str_replace("<br>", "\n", $kundenadresse)), TEXT_PDF_SHOPADRESSEKLEIN);

if(file_exists(DIR_FS_CATALOG.'images/pdf_logo.jpg'))
	$pdf->Logo(DIR_FS_CATALOG.'images/pdf_logo.jpg');

preg_match("/(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2}):(\d{2})/", $order->info['date_purchased'], $dt);
// aktuelle Datum
$dt = explode('-', ' -'.strftime('%Y-%m-%d'));

if ($order->info['payment_method'] !='' && $order->info['payment_method']!='no_payment') {
	include(DIR_FS_CATALOG.'lang/'.$sprache.'/modules/payment/'.$order->info['payment_method'].'.php');
 	$payment_method = constant(strtoupper('MODULE_PAYMENT_'.$order->info['payment_method'].'_TEXT_TITLE'));
} else
	$payment_method = '';

$pdf->Rechnungsdaten($order->customer['cID'], $_GET['oID'], $_POST['pdf_bill_nr'], $dt[3].'.'.$dt[2].'.'.$dt[1], html_entity_decode($payment_method), PDF_LIEFERSCHEIN);
$pdf->RechnungStart($_POST['pdf_bill_nr'], utf8_decode($order->customer['lastname']), $customer_gender->fields['customers_gender'], PDF_LIEFERSCHEIN);
$pdf->ListeKopf(PDF_LIEFERSCHEIN);

$order_data_values = $db->db_query("SELECT
										products_id,
										orders_products_id,
										products_model,
										products_name,
										products_price,
										final_price,
										products_quantity
									FROM 
										".TABLE_ORDERS_PRODUCTS."
									WHERE 
										orders_id='".(int)$_GET['oID']."'");

$order_data = array();

while (!$order_data_values->EOF) {
    $attributes_data_values = $db->db_query("SELECT
												products_options,
												products_options_values,
												price_prefix,
												options_values_price
											FROM
												".TABLE_ORDERS_PRODUCTS_ATTRIBUTES."
											WHERE
												orders_products_id='".$order_data_values->fields['orders_products_id']."'");

    $attributes_data='';
	$attributes_model='';

    while (!$attributes_data_values->EOF) {
        $attributes_data .= $attributes_data_values->fields['products_options'].': '.$attributes_data_values->fields['products_options_values']."\n";
		$attributes_model .= get_attributes_model($order_data_values->fields['products_id'],$attributes_data_values->fields['products_options_values'],$attributes_data_values->fields['products_options'])."\n";
   		$attributes_data_values->MoveNext();
     }

	$pdf->ListeProduktHinzu($order_data_values->fields['products_quantity'],
							utf8_decode($order_data_values->fields['products_name']),
							utf8_decode(trim($attributes_data)),
							$order_data_values->fields['products_model'],
							trim($attributes_model, $type),
							str_replace('€', chr(128), $pdf_price->format($order_data_values->fields['products_price'], true)),
							str_replace('€', chr(128), $pdf_price->format($order_data_values->fields['final_price'], true)),
							PDF_LIEFERSCHEIN);
				
	$order_data_values->MoveNext();
}
if(!PDF_LIEFERSCHEIN) {
	$oder_total_values = $db->db_query("SELECT
											  title,
											  text,
											  class,
											  value,
											  sort_order
										FROM
											".TABLE_ORDERS_TOTAL."
										WHERE
											orders_id='".$_GET['oID']."'
										ORDER BY
											sort_order ASC");

	$order_data = array();

	while (!$oder_total_values->EOF){
		$order_data[] = array('title' => utf8_encode($oder_total_values->fields['title']), 
								'class'=> $oder_total_values->fields['class'], 
								'value'=> $oder_total_values->fields['value'], 
								'text' => str_replace('€', chr(128), $oder_total_values->fields['text']));
		$oder_total_values->MoveNext();
	}
	$pdf->Betrag($order_data, PDF_LIEFERSCHEIN);
}

$pdf->RechnungEnde($order->customer['vat_id'], PDF_LIEFERSCHEIN);

$pdf->Kommentar($order->info['comments'], $order->info['shipping_method'], PDF_LIEFERSCHEIN);

if(!PDF_LIEFERSCHEIN) {
	$pdf_name = get_pdf($_POST['oID'], true).'.pdf';
	$pdf->Output($pdf_name, 'F');
	$check_pdf = $db->db_query("SELECT bill_name FROM ".TABLE_ORDERS_PDF." WHERE order_id = '".$_POST['oID']."'");
	if($check_pdf->_numOfRows)
		$db->db_query("UPDATE ".TABLE_ORDERS_PDF." SET bill_name = '".$pdf_name."', pdf_bill_nr = '".$_POST['pdf_bill_nr']."', pdf_generate_date = NOW() WHERE order_id = '".$_POST['oID']."' ");
	else
		$db->db_query("INSERT INTO ".TABLE_ORDERS_PDF." (order_id, bill_name, pdf_bill_nr, pdf_generate_date) VALUES ('".$_POST['oID']."', '".$pdf_name."', '".$_POST['pdf_bill_nr']."', NOW());");
	$_SESSION['msg']['gp'] = '<div class="success_msg">Die PDF Rechnung wurde erfolgreich erstellt.</div>';
	if($_POST['pdf_rechnung_senden'] !='1')
		redirect(href_link(FILENAME_ORDERS, 'page='.$_POST['page'].'&action=edit&oID='.$_POST['oID'].'#pdf'));
		
} else {
	$pdf_name = str_replace('#bn#', (int)$_GET['oID'], TEXT_PDF_DELIVERY_FILE_NAME);
	$pdf_name = str_replace('#vn#', $order->customer['firstname'], $pdf_name);
	$pdf_name = str_replace('#nn#', $order->customer['lastname'], $pdf_name);
	$pdf_name = str_replace('#d#', date("d-m-Y"), $pdf_name).'.pdf';

	header("Content-Disposition: attachment; filename=\"".$pdf_name."\"");
	header("Content-type: application/pdf");
	$pdf->Output($pdf_name, 'D');
}